<template>
  <div class="app-container">
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="发料单编号" prop="issueCode">
        <el-input
          v-model="queryParams.issueCode"
          placeholder="请输入发料单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外协工单编码" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入外协工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="供应商名称" prop="vendorName">
        <el-input
          v-model="queryParams.vendorName"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发料日期" prop="issueDate">
        <el-date-picker clearable
          v-model="queryParams.issueDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发料日期">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:wm:outsourceissue:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="outsourceissueList" @selection-change="handleSelectionChange">
      <el-table-column label="发料单编号" align="center" prop="issueCode" >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:wm:outsourceissue:query']"
          >{{scope.row.issueCode}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="发料单名称" align="center" prop="issueName" />
      <el-table-column label="生产工单编码" align="center" prop="workorderCode" />
<!--      <el-table-column label="供应商名称" align="center" prop="vendorName" />-->
      <el-table-column label="发料日期" align="center" prop="issueDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.issueDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-video-play"
              v-if="scope.row.status == 'PREPARE'"
              @click="handleExecute(scope.row)"
              v-hasPermi="['mes:wm:outsourceissue:execute']"
            >执行发料</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-if="scope.row.status == 'PREPARE'"
              v-hasPermi="['mes:wm:outsourceissue:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="scope.row.status == 'PREPARE'"
              v-hasPermi="['mes:wm:outsourceissue:remove']"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <!-- 添加或修改外协领料单头对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :disabled="optType!='add'&&optType!='edit'" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="发料单编号" prop="issueCode">
              <el-input v-model="form.issueCode" :readonly="optType != 'add'" placeholder="请输入发料单编号" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="发料单名称" prop="issueName">
              <el-input v-model="form.issueName" placeholder="请输入发料单名称" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
             <el-form-item label="发料日期" prop="issueDate">
              <el-date-picker clearable
                v-model="form.issueDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择发料日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">
            <el-form-item label="发料仓库">
              <el-cascader v-model="warehouseInfo"
                :options="warehouseOptions"
                :props="warehouseProps"
                @change="handleWarehouseChanged"
              >
              </el-cascader>
            </el-form-item>
          </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单据状态" prop="status">
              <el-select v-model="form.status" disabled>
                <el-option
                  v-for="dict in dict.type.mes_order_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="外协工单" prop="workorderCode">
              <el-input v-model="form.workorderCode" placeholder="请选择外协工单" readonly>
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  :disabled="optType != 'add'"
                  @click="handleWorkorderSelect">
                </el-button>
              </el-input>
            </el-form-item>
            <OutsourceissueSelect
              ref="woSelect"

              @onSelected="onWorkorderSelected">
            </OutsourceissueSelect>
          </el-col>
          <el-col :span="8">
            <el-form-item label="工单数量" prop="attr1">
              <el-input v-model="form.attr1" readonly="readonly">
              </el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">
            <el-form-item label="供应商" prop="vendorCode">
              <el-input v-model="form.vendorCode" readonly="readonly">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="供应商名称" >
              <el-input v-model="form.vendorName" placeholder="请选择外协工单" >
              </el-input>
            </el-form-item>
          </el-col>-->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-divider content-position="center"  v-if="form.attr3&&form.attr3!=0">物料清单</el-divider>
      <el-card shadow="always"  v-if="form.attr3&&form.attr3!=0" class="box-card">
        <el-table border v-loading="false" :data="itemList" >
          <el-table-column type="index" label="序号" width="55" align="center"/>
          <el-table-column label="物料编码" width="140"align="center" prop="itemCode" :show-overflow-tooltip="true"/>
          <el-table-column label="物料名称" width="140" align="center" prop="itemName" :show-overflow-tooltip="true"/>
          <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
          <el-table-column label="单位" width="50" align="center" prop="unitOfMeasure" :show-overflow-tooltip="true"/>
          <el-table-column
            v-for="col in columnsConfig"
            :key="col.prop"
            :label="col.label"
            :prop="col.prop"
            :width="col.width"
            :align="col.align"
            v-if="col.show">
          </el-table-column>
          <el-table-column label="需求数量" width="120px" align="center" prop="used" :show-overflow-tooltip="true"/>
          <el-table-column label="已发料数量" width="120px" align="center" prop="haveIssued" :show-overflow-tooltip="true"/>
          <el-table-column label="剩余数量" width="120px" align="center" prop="left" :show-overflow-tooltip="true"/>
          <!--          <el-table-column label="用料比例" width="100px" align="center" prop="proportional"/>-->
          <el-table-column label="规格" align="center" prop="model" :show-overflow-tooltip="true"/>
          <el-table-column label="规格单位" align="center" width="80" prop="specificationUnit"/>
          <el-table-column label="操作" align="center" fixed="right" v-if="isExecute==true">
            <template slot-scope="scope">
              <el-tooltip content="选择" placement="top">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-circle-check"
                  @click="handleSelect(scope.row)"
                  v-hasPermi="['mes:wm:outsourceissue:execute']"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="itemListTotal"
          :page.sync="itemListQueryParams.pageNum"
          :limit.sync="itemListQueryParams.pageSize"
          @pagination="getBomItemList"
        />
      </el-card>
      <el-divider v-if="form.issueId != null&&(isExecute||optType=='view')" content-position="center">出库信息</el-divider>
      <el-card shadow="always" v-if="form.issueId != null&&(isExecute||optType=='view')" class="box-card">
        <Issueline
          ref="line"
          :issue-id="form.issueId"
          :isExecute="isExecute"
          :bom-id="form.attr3"
          :optType="optType"
        @child-list-updated="compareList">
        </Issueline>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="executeForm" v-hasPermi="['mes:wm:outsourceissue:execute']" v-if="isExecute">执 行</el-button>
        <el-button type="primary" @click="submitForm" v-if="form.status == 'PREPARE' && optType != 'view' ">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addOutsourceissue, delOutsourceissue, execute, getOutsourceissue, listOutsourceissue, updateOutsourceissue,sumQuantityIssued } from "@/api/mes/wm/outsourceissue";
import OutsourceissueSelect from "@/components/OutsourceissueSelect/single.vue"
import { getTreeList } from "@/api/mes/wm/warehouse"
import { genCode } from "@/api/system/autocode/rule"
import Issueline from "./line.vue";
import PlanBom from "@/views/mes/pro/planning/bom/bom.vue";
import { listItem } from "@/api/mes/md/bomItem";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";
export default {
  name: "Outsourceissue",
  dicts: ['mes_order_status'],
  components: {PlanBom, Issueline, OutsourceissueSelect},
  data() {
    return {
      columnsConfig:[],
      itemList:[],
      openSelect:false,
      optType: undefined,
      isExecute:false,
      warehouseInfo:[],
      warehouseOptions:[],
      warehouseProps:{
        multiple: false,
        value: 'pId',
        label: 'pName',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外协领料单头表格数据
      outsourceissueList: [],

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //工单弹出框结果过滤参数
     // workorder: { workorderType: 'OUTSOURCE'},
      itemListTotal: 0,
      itemListQueryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderId: null,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        issueCode: null,
        issueName: null,
        workorderId: null,
        workorderCode: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        vendorNick: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
        issueDate: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        issueCode: [
          { required: true, message: "发料单编号不能为空", trigger: "blur" }
        ],
        workorderCode: [
          { required: true, message: "请选择外协工单", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
    this.getItemBase();
  },
  watch:{

  },
  methods: {
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
    /** 查询外协领料单头列表 */
    getList() {
      this.loading = true;
      listOutsourceissue(this.queryParams).then(response => {
        this.outsourceissueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getWarehouseList(){
      getTreeList().then( response =>{
        if(response.data){
          this.warehouseOptions = response.data.filter((el) => {
              return el.warehouseCode.indexOf('VIR') == -1;
          });
        }
        this.warehouseOptions.map(w =>{
          w.children.map(l =>{
                  let lstr =JSON.stringify(l.children)
                    .replace(/locationId/g,'lId')
                    .replace(/areaId/g, 'pId')
                    .replace(/areaName/g,'pName');
                  l.children = JSON.parse(lstr);
          });

          let wstr = JSON.stringify(w.children)
            .replace(/warehouseId/g,'wId')
            .replace(/locationId/g, 'pId')
            .replace(/locationName/g,'pName');
          w.children =  JSON.parse(wstr);

        });
        let ostr=JSON.stringify(this.warehouseOptions)
          .replace(/warehouseId/g,'pId')
          .replace(/warehouseName/g, 'pName');
        this.warehouseOptions = JSON.parse(ostr);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        issueId: null,
        issueCode: null,
        issueName: null,
        workorderId: null,
        workorderCode: null,
        vendorId: null,
        vendorCode: null,
        vendorName: null,
        vendorNick: null,
        warehouseId: null,
        warehouseCode: null,
        warehouseName: null,
        locationId: null,
        locationCode: null,
        locationName: null,
        areaId: null,
        areaCode: null,
        areaName: null,
        issueDate: new Date(),
        status: "PREPARE",
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        quantity: null
      };
      this.isExecute=false;
      this.resetForm("form");
      this.itemList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelect( row){
      this.$refs.line.itemCode=row.itemCode;
      this.$refs.line.itemId=row.itemId;
      this.$refs.line.enableMultiUnit=row.enableMultiUnit;
      this.$refs.line.storageUnitType=row.storageUnitType;
      this.$refs.line.storageRatio=row.storageRatio;
      this.$refs.line.storageUnit=row.storageUnit;
      this.$refs.line.issueValue=new Number(row.left)<0?0.00:new Number(row.left);
      this.$refs.line.handleAdd();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.issueId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.reset();
      genCode('OUTSOURCE_ISSUE_CODE').then(response =>{
        this.form.issueCode = response;
      });
      this.title = "添加外协发料单头";
      this.optType = "add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const issueId = row.issueId || this.ids
      getOutsourceissue(issueId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改外协发料单头";
        this.optType = "edit";
        this.getBomItemList();
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.issueId != null) {
            updateOutsourceissue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOutsourceissue(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const issueIds = row.issueId || this.ids;
      this.$modal.confirm('是否确认删除外协发料编号为"' + row.issueCode + '"的数据项？').then(function() {
        return delOutsourceissue(issueIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 查询明细按钮操作
    handleView(row){
      this.reset();
      const issueIds = row.issueId
      getOutsourceissue(issueIds).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看外协发料单信息";
        this.optType = "view";
        this.getBomItemList();
      });
    },
    executeForm(){
      if(this.$refs.line.outsourceissuelineList.length>0){
        execute(this.form.issueId).then(response => {
          this.$modal.msgSuccess("执行成功");
          this.open = false;
          this.getList();
        })
      }else{
        this.$modal.msgError("请先添加出库信息");
      }
    },
    //执行发料
    handleExecute(row){
      this.reset();
      const issueIds = row.issueId
      getOutsourceissue(issueIds).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "执行外协发料单信息";
        this.optType = "view";
        this.isExecute=true;
        this.getBomItemList();
      });

    },
    //选择生产工单
    handleWorkorderSelect(){
      this.$refs.woSelect.showFlag = true;
    },
    onWorkorderSelected(row){
      if(row != undefined && row != null){
        this.form.workorderId = row.workorderId;
        this.form.workorderCode = row.workorderCode;
        this.form.workorderName = row.workorderName;
        this.form.attr3 = row.bomId;
        this.form.attr1 = row.quantity;
        this.form.vendorId = row.vendorId;
        this.form.vendorCode = row.vendorCode;
        this.form.vendorName = row.vendorName;
        this.form.quantity = row.quantity;
        this.getBomItemList();
      }
    },
    // 获取物料组成列表
    getBomItemList() {
      if(this.form.attr3) {
        this.itemListQueryParams.parentId = this.form.attr3;
        listItem(this.itemListQueryParams).then(response => {
          if (response.code == 200 && response.rows) {
            this.itemList = response.rows.map((item) => {
              item.used = (item.used * this.form.attr1).toFixed(2);
              item.you=false;
              item.haveIssued=(0).toFixed(2);
              item.left=(item.used-item.haveIssued).toFixed(2);
              return item;
            })
            sumQuantityIssued(this.form.workorderId).then(re=>{
              if(re.code==200&&re.data){
                this.itemList = response.rows.map((item) => {
                  re.data.some(it=>{
                    if(item.itemId==it.itemId){
                      item.haveIssued=it.quantityIssued.toFixed(2);
                      item.left=(item.used-item.haveIssued).toFixed(2);
                      return true;
                    }
                  })
                  return item;
                })
              }
            })

            this.itemListTotal = response.total;
          }
        })
      }else{
        this.itemList=[];
      }
    },
    compareList(childList){
      if(this.form.attr3){
        if(this.itemList.length>0){
          this.itemList=this.itemList.map((item)=>{
            if(childList.length>0) {
              if (childList.find(childItem => childItem.itemCode == item.itemCode)) {
                item.you = true;
              } else {
                item.you = false;
              }
              return item;
            } else {
              item.you = false;
              return item;
            }
          })

        }
      }
    },
    //供应商选择
    handleSelectVendor(){
      this.$refs.vendorSelect.showFlag = true;
    },
    //供应商选择弹出框
    onVendorSelected(obj){
        if(obj != undefined && obj != null){
          this.form.vendorId = obj.vendorId;
          this.form.vendorCode = obj.vendorCode;
          this.form.vendorName = obj.vendorName;
          this.form.vendorNick = obj.vendorNick;
        }
    },

    //选择默认的仓库、库区、库位
    handleWarehouseChanged(obj) {
      if (obj != null) {
        this.form.warehouseId = obj[ 0 ];
        this.form.locationId = obj[ 1 ];
        this.form.areaId = obj[ 2 ];
      }
    },
  }
};
</script>
